$.fn.serializeObject = function()    
{    
   var o = {};    
   var a = this.serializeArray();    
   $.each(a, function() {    
       if (o[this.name]) {    
           if (!o[this.name].push) {    
               o[this.name] = [o[this.name]];    
           }    
           o[this.name].push(this.value || '');    
       } else {    
           o[this.name] = this.value || '';    
       }    
   });    
   return o;    
};  

var params = {};
var current = {};
var defaultPageSize = 10;
var currentPage = 1;
var pageSize = defaultPageSize;
var option = {};


$("ul.tabtit").on("click",function(event){
	$('.main_panel').load(webUrl+"public/page/materiel/materielQuery.html");
});

$(function(){
	var id=localStorage.getItem("rowId");
	localStorage.removeItem("rowId");
	getMaterielById(id);
	
	$('.add').on('click',saveScene);
	$('.empty').on('click',back);
	$('.check').on('click',queryLov);
	$('.copy').on('click',queryMaterielLov);
});

//物料类别需要用到的变量
var matParams = {};
var matCurrent = {};
var matDefaultPageSize = 10;
var matCurrentPage = 1;
var matPageSize = matDefaultPageSize;
var matOption = {};
function materielTypeNameClick(){
	matOption.url = webUrl+'sysParams/getSysParamsList.do';
	matOption.type="materielType";				//div名称前缀user_dialogbox
	matOption.table="materielTypeGrid";	//div中table表名
	matOption.obj="materiel";				//回调方法中传值做标识
	matParams.typeCode="MaterielCode";
	var paramValue=$("#param_Value").val();
	var note=$("#note").val();
	if(paramValue!=""){
		matParams.paramValue=paramValue;
	}
	if(note!=""){
		matParams.note=note;
	}
	matOption.columns = [
	                  {field:'id',checkbox:true},
			          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
			          {field:'param_value',title:'物料类别名称',align:'center',width:150,sortable:true},
			          {field:'note',title:'备注',align:'center',width:150,sortable:true},
			      ];
	matOption.onSelect=function(rowIndex, rowData){ $("#materielTypeName").val(rowData.param_value); $("#materielTypeId").val(rowData.id);};
	matLov(matOption);
}


function vendorNameClick(){
	option.url = webUrl+'chwVendor/findchwVendor.do';
	option.type="user";				//div名称前缀user_dialogbox
	option.table="customerGrid";	//div中table表名
	option.obj="wuliao";				//回调方法中传值做标识
	option.columns = [
	                  {field:'vendorId',checkbox:true},
			          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
			          {field:'vendorName',title:'供应商名称',align:'center',width:150,sortable:true},
			          {field:'vendorContactName',title:'供应商联系人',align:'center',width:150,sortable:true},
			          {field:'remark',title:'备注',align:'center',width:150,sortable:true},
			      ];
	option.onSelect=function(rowIndex, rowData){ $("#vendorName").val(rowData.vendorName); $("#vendorId").val(rowData.vendorId);};
	Lov(option);
}

function getMaterielById(id){
	$.ajax({
		url:webUrl+'chwMateriel/findChwMaterielById.notPermSc',
		type:'post',
		data:{id:id}
	}).done(function(data){
		var d = data.bo;
		//alert(JSON.stringify(d));
		$("#segment1").val(d.segment1);
		$("#materielName").val(d.materielName);
		$("#materielTypeId").val(d.materielTypeId);
		$("#materielTypeName").val(d.materielTypeName);
		$("#companyId").val(d.companyId);
		$("#materielId").val(d.materielId);
		$("#companyName").val(d.companyName);
		$("#materielVersionNum").val(d.materielVersionNum);
		$("#materielVersionName").val(d.materielVersionName);
		$("#materielUnitPrice").val(d.materielUnitPrice);
		$("#materielMarkedPrice").val(d.materielMarkedPrice);
		$("#materielUnit").val(d.materielUnit);
		$("#materielCurtainBreadth").val(d.materielCurtainBreadth);
		$("#materielFullvolumePrice").val(d.materielFullvolumePrice);
		$("#materielCutopenPrice").val(d.materielCutopenPrice);
		$("#vendorId").val(d.vendorId);
		$("#vendorName").val(d.vendorName);
		$("#remark").val(d.remark);
		$("#materielVersionModel").val(d.materielVersionModel);
		$("#materielSpace").val(d.materielSpace);
	});
}
function queryLov(){
	if($("#vendorNameLov").val()!=""){
		params.vendorName=$("#vendorNameLov").val();
	}
	if($("#vendorContactNameLov").val()!=""){
		params.vendorContactName=$("#vendorContactNameLov").val();
	}
	option.url = webUrl+'chwVendor/findchwVendor.do';
	option.type="user";				//div名称前缀user_dialogbox
	option.table="customerGrid";	//div中table表名
	option.obj="wuliao";				//回调方法中传值做标识
	option.columns = [
	                  {field:'vendorId',checkbox:true},
			          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
			          {field:'vendorName',title:'供应商名称',align:'center',width:150,sortable:true},
			          {field:'vendorContactName',title:'供应商联系人',align:'center',width:150,sortable:true},
			          {field:'remark',title:'备注',align:'center',width:150,sortable:true},
			      ];
	option.onSelect=function(rowIndex, rowData){ $("#vendorName").val(rowData.vendorName); $("#vendorId").val(rowData.vendorId);};
	Lov(option);
}

function queryMaterielLov(){
	materielTypeNameClick();
}

function goback(obj){
	if(obj=="wuliao"){
		var vendorId= $('input[name="vendorId"]:checked').val();
		$("#vendorId").val(vendorId);
	}
}

function back(){
	$('.main_panel').load(webUrl+"public/page/materiel/materielQuery.html");
}

function saveScene(){
	var json={};
    json = $("#addDataFormHeader").serializeObject();
    json=JSON.stringify(json);
	if(checknull("segment1") && checknull("materielName")){
		$.ajax({
			url: webUrl+'chwMateriel/updateChwMateriel.do',
			type:'post',
			data:{param:json}
		}).done(function(data){
			var result = data.bo;
			if(result==0){
				$.messager.confirm('提示 ', '修改成功！', function(r){
					$('.main_panel').load(webUrl+"public/page/materiel/materielQuery.html");
				});
			}else{
				$.messager.alert('提示 ', '修改失败！', 'info');
			}
		});
	}
}


